$(document).ready(function() {
    get_hash();
	$("#submitObDir").click(function(){
		saveObDir();
	});
	$("#add_obdir_form").click(function(){
		addObDirForm();
	});
	$("#addsubmitObDir").click(function(){
		addObDir();
	});
	$(".radioVal1").click(function() { 
		radioVal1List = [];
	    var radioVal1='';
	    $('.radioVal1:checked').each(function() {
	    	radioVal1List.push($(this).val());
	    	radioVal1+=$(this).val();
	    });
	    $("#ampmVal1").val(radioVal1);
	});
	$(".radioVal2").click(function() { 
		radioVal2List = [];
	    var radioVal2='';
	    $('.radioVal2:checked').each(function() {
	    	radioVal2List.push($(this).val());
	    	radioVal2+=$(this).val();
	    });
	    $("#ampmVal2").val(radioVal2);
	});
	$('#strObDirTime1').change(function() {
		var tVal1 = $(this).val();
		$("#timeVal1").val(tVal1);
	});
	$('#strObDirTime2').change(function() {
		var tVal2 = $(this).val();
		$("#timeVal2").val(tVal2);
	});
	

	$(".addradioVal1").click(function() { 
		addradioVal1List = [];
	    var addradioVal1='';
	    $('.addradioVal1:checked').each(function() {
	    	addradioVal1List.push($(this).val());
	    	addradioVal1+=$(this).val();
	    });
	    $("#addampmVal1").val(addradioVal1);
	});
	$(".addradioVal2").click(function() { 
		addradioVal2List = [];
	    var addradioVal2='';
	    $('.addradioVal2:checked').each(function() {
	    	addradioVal2List.push($(this).val());
	    	addradioVal2+=$(this).val();
	    });
	    $("#addampmVal2").val(addradioVal2);
	});
	$('#addstrObDirTime1').change(function() {
		var addtVal1 = $(this).val();
		$("#addtimeVal1").val(addtVal1);
	});
	$('#addstrObDirTime2').change(function() {
		var addtVal2 = $(this).val();
		$("#addtimeVal2").val(addtVal2);
	});
});
function obgyne_directory(){
	var asort = 0;
	obgyne_dir_icons = {};
	xfunc = "obgyne_directory/get_list_obgyne_directory";
	
	data_sent = {};
		
		$.ajax({ 
			type: "POST", // request type POST
			url: base_url+xfunc, // post to PHP file
			data: data_sent, // request send to PHP file
			dataType:'json',
			success: function(result){ // result = return from php request
				obgyne_dir_icons_panel = '';
				$(result.intID).each(function(k,v){
					//asort = result.intID[k];
					//obgyne_dir_icons_panel = '';
						obgyne_dir_icons_panel +='<tr>';
							obgyne_dir_icons_panel +='<td>'+result.strName[k]+'</td>';
							obgyne_dir_icons_panel +='<td>'+result.strLocation[k]+'</td>';
							obgyne_dir_icons_panel +='<td>'+result.strDays[k]+'</td>';
							obgyne_dir_icons_panel +='<td>'+result.strTime[k]+'</td>';
							obgyne_dir_icons_panel +='<td class="controls"><div class="btn-group">';
							obgyne_dir_icons_panel +='<button class="btn btn-primary" onclick="javascript:editObDir(\''+result.intID[k]+'\')"><i class="icon-edit"></i>Edit</button>';
							obgyne_dir_icons_panel +='<button class="btn btn-danger" onclick="javascript:delObDir(\''+result.intID[k]+'\',\''+result.strName[k]+'\')"><i class="icon-trash"></i>Delete</button>';
							obgyne_dir_icons_panel +='</td>';
							obgyne_dir_icons_panel +='</tr>';
						obgyne_dir_icons[result.intID[k]]=obgyne_dir_icons_panel;
				});
				
				obgyne_dir_html_panel = '<table class="table table-striped table-bordered" id="obgyne_dir_table">';
				obgyne_dir_html_panel += '<thead>';
					obgyne_dir_html_panel += '<tr>';
						obgyne_dir_html_panel += '<th class="note-title">OB-Gyne Name</th>';
						obgyne_dir_html_panel += '<th class="note-title">Location</th>';
						obgyne_dir_html_panel += '<th class="note-title">Days</th>';
						obgyne_dir_html_panel += '<th class="note-title">Time</th>';
						obgyne_dir_html_panel += '<th>Control</th>';
					obgyne_dir_html_panel += '</tr>';
				obgyne_dir_html_panel += '</thead>';
				obgyne_dir_html_panel += '<tbody>';
				obgyne_dir_html_panel += obgyne_dir_icons_panel;
//				for(x=0;x<=asort;x++){
//					if($.trim(obgyne_dir_icons[x])!=""){
//						obgyne_dir_html_panel +=obgyne_dir_icons[x];
//					}
//								
//				}		
				obgyne_dir_html_panel += '</tbody>';		
				obgyne_dir_html_panel += '</table>';
				
				$('#obgyne_dir_content').html(obgyne_dir_html_panel);
				$('#obgyne_dir_table').dataTable({
					"sPaginationType": "full_numbers",
					"bLengthChange": false,
					"bSort": false
				});
			}
		});	
}
function addObDirForm(){
	$("#obgyne_dir_content, #add_obdir_form").hide();
	$("#addObDir").slideDown('slow', function(){
		$("#obgyne_dir_content").hide();
		$("#addObDir").slideDown('slow', function(){
			$(".back-btn-addobdir a").attr("onclick","btn_back_addobdir()");
			var days = {
					'Sunday':'SN',
					'Monday':'M',
					'Tuesday':'T',
					'Wednesday':'W',
					'Thursday':'TH',
					'Friday':'F',
					'Saturday':'SA'
			};
			var dayVal = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
			var html = '';
			html = '<ul>';
			for(x=0;x<=6;x++){
				html += '<li>';
					html += '<input type="checkbox" class="addchkbx" id="addday-'+dayVal[x]+'" value="'+days[dayVal[x]]+'" />';
					html += '<label for="addday-'+dayVal[x]+'">'+dayVal[x]+'</label>';
				html += '</li>';
			}
			html += '</ul>';
			$("#addstrObDirDays").html(html);
			$(".addchkbx").click(function() { 
			    dayList = [];
			    var arr='';
			    $('.addchkbx:checked').each(function() {
			    	dayList.push($(this).val());
			        arr+=$(this).val();
			    });
			    $("#adddayVal").val(arr);
			});
		});	
	});	
}

function addObDir(){
	var strName = $("#addstrObDirName").val();
	var strLocation = $("#addstrObDirLocation").val();
	var strSalutation = $("#addstrSalutation").val();
	var strDays = $("#adddayVal").val();
	var strTime1 = $("#addtimeVal1").val();
	var strTime2 = $("#addtimeVal2").val();
	var ampmVal1 = $("#addampmVal1").val();
	var ampmVal2 = $("#addampmVal2").val();
	var timeSpan = strTime1+''+ampmVal1+'-'+strTime2+''+ampmVal2;
	
	if(strName==''){
		$("#errorObDir").html("<div class='alert alert-error'>Error: Put the name!</div>");
	} else if(strLocation=='') {
		$("#errorObDir").html("<div class='alert alert-error'>Error: Put the location!</div>");
	} else if(strTime1=='') {
		$("#errorObDir").html("<div class='alert alert-error'>Error: Select time!</div>");
	} else if(strTime2=='') {
		$("#errorObDir").html("<div class='alert alert-error'>Error: Select time!</div>");
	} else if(ampmVal1=='') {
		$("#errorObDir").html("<div class='alert alert-error'>Error: Select morning or afternoon!</div>");
	} else if(ampmVal2=='') {
		$("#errorObDir").html("<div class='alert alert-error'>Error: Select morning or afternoon!</div>");
	} else {
		xfunc = "obgyne_directory/save_obgyne_dir";
		data_sent = {
				'strSalutation':strSalutation,
				'strName':strName,
				'strLocation':strLocation,
				'strDays':strDays,
				'timeSpan':timeSpan
		};
	
		$.ajax({
			type: 'POST',
			url: base_url+xfunc,
			data: data_sent, 
			dataType:'json',
			success: function(result){
				$("#errorObDir").hide();
				$("#addObDirSuccess").html('<div class="alert alert-success">Data was successfully saved!</div>');
				
				$("#addstrObDirName").val('');
				$("#addstrObDirLocation").val('');
				$(".alert-success").fadeOut(5000);
			}
		});
	}
}
function editObDir(noteID){
	xfunc = "obgyne_directory/get_obgyne_dir";
	data_sent = {
			'intID':noteID
	};
	$.ajax({
		type: 'POST',
		url: base_url+xfunc,
		data: data_sent, 
		dataType:'json',
		success: function(result){
			var days = {
					'Sunday':'SN',
					'Monday':'M',
					'Tuesday':'T',
					'Wednesday':'W',
					'Thursday':'TH',
					'Friday':'F',
					'Saturday':'SA'
			};
			var dayVal = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
			var html = '';
			html = '<ul>';
			for(x=0;x<=6;x++){
				html += '<li>';
					html += '<input type="checkbox" class="chkbx" id="day-'+dayVal[x]+'" value="'+days[dayVal[x]]+'" />';
					html += '<label for="day-'+dayVal[x]+'">'+dayVal[x]+'</label>';
				html += '</li>';
			}
			html += '</ul>';
			$("#intObDirID").val(result.intID);
			$("#intOBDirectoryID").val(result.intOBDirectoryID);
			$("#strObDirName").val(result.strName);
			$("#strObDirLocation").val(result.strLocation);
			$("#strObDirDays").html(html);
			$("#strObDirTime").val(result.strTime);
			$(".chkbx").click(function() { 
			    dayList = [];
			    var arr='';
			    $('.chkbx:checked').each(function() {
			    	dayList.push($(this).val());
			        arr+=$(this).val();
			    });
			    $("#dayVal").val(arr);
			});
		}
	});

	$("#add_obdir_form").hide();
	$("#obgyne_dir_content").hide();
	$("#editObDir").slideDown('slow', function(){
		$(".back-btn-obdir a").attr("onclick","btn_back_obdir()");
	});	
}
function saveObDir(){
	var intID = $("#intObDirID").val();
	var intOBDirectoryID = $("#intOBDirectoryID").val();
	var strName = $("#strObDirName").val();
	var editstrSalutation = $("#editstrSalutation").val();
	var strLocation = $("#strObDirLocation").val();
	var strDays = $("#dayVal").val();
	var strTime1 = $("#timeVal1").val();
	var strTime2 = $("#timeVal2").val();
	var ampmVal1 = $("#ampmVal1").val();
	var ampmVal2 = $("#ampmVal2").val();
	var timeSpan = strTime1+''+ampmVal1+'-'+strTime2+''+ampmVal2;
	
	xfunc = "obgyne_directory/save_obgyne_dir";
	data_sent = {
			'intID':intID,
			'intOBDirectoryID':intOBDirectoryID,
			'strSalutation':editstrSalutation,
			'strLocation':strLocation,
			'strDays':strDays,
			'timeSpan':timeSpan,
			'strName':strName
	};
	$.ajax({
		type: 'POST',
		url: base_url+xfunc,
		data: data_sent, 
		dataType:'json',
		success: function(result){
			$("#ObDirSuccess").html('<div class="alert alert-success">Data was successfully updated!</div>');
			$(".alert-success").fadeOut(5000);
		}
	});
}
function delObDir(noteID, titleName){
	xfunc = "obgyne_directory/delete_obgyne_dir";
	data_sent = {
			'intID':noteID
	};
	$('#label-delete').html(titleName);
    $('#deleteModal').modal('show');
    $('button#confirm').click(function(e){
        $('#deleteModal').modal('hide');
		
		$.ajax({
			type: 'POST',
			url: base_url+xfunc,
			data: data_sent, 
			dataType:'json',
			success: function(result){
				obgyne_directory();
			}
		});
    });
}
function btn_back_addobdir(){
	$("#addObDir").hide();
	$("#obgyne_dir_content").slideDown('slow', function(){
		$("#add_obdir_form").show();
		obgyne_directory();
	});	
}
function btn_back_obdir(){
	$("#editObDir").hide();
	$("#obgyne_dir_content").slideDown('slow', function(){
		$("#add_obdir_form").show();
		obgyne_directory();
	});	
}